#pragma once
#include <iostream>
#include <vector>
#include <unistd.h>
#include <functional>

using task_t = std::function<void()>;

void Download()
{
    std::cout << "我是一个下载任务"
              << " 处理者: " << getpid() << std::endl;
}

void PrintLog()
{
    std::cout << "我是一个打印日志的任务"
              << " 处理者: " << getpid() << std::endl;
}

void PushVideoStream()
{
    std::cout << "这是一个推送视频流的任务"
              << " 处理者: " << getpid() << std::endl;
}

// 一个进程可能要执行多种不同的任务
class Init
{
public:
    Init()
    {
        tasks.push_back(Download);
        tasks.push_back(PrintLog);
        tasks.push_back(PushVideoStream);
        srand(time(nullptr) ^ getpid());
    }

    bool checkCode(int code)
    {
        if (code >= 0 && code < tasks.size())
            return true;
        else
            return false;
    }

    void runTask(int code)
    {
        return tasks[code]();
    }
    int selectTask()
    {
        return rand() % tasks.size();
    }

    std::string ToDesc(int code)
    {
        switch (code)
        {
        case g_download_code:
            return "Download";
        case g_printLog_code:
            return "PrintLog";
        case g_pushVideoStream_code:
            return "PushVideoStream";
        default:
            return "Unknow";
        }
    }

    public:
        // 任务码
        const static int g_download_code = 0;
        const static int g_printLog_code = 1;
        const static int g_pushVideoStream_code = 2;
        // 任务集合
        std::vector<task_t> tasks;
    };
    Init init; // 定义对象